package view.table;

import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

import entiey.Table;
import service.TableService;
import service.impl.TableServiceImpl;
import util.CastUtil;

import java.awt.Toolkit;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;

public class V_tables extends JFrame {

	private JPanel contentPane;
	private TableService ts=new TableServiceImpl();
	private static JPanel this_panle;
	private Table table;
	private JPanel panel;
	private JPanel panel_2;
	private JLabel labelIcon;
	private JLabel label_1;
	private File fileIcon;
	private int tableId;
	
	public int getTableId() {
		return tableId;
	}

	public void setTableId(int tableId) {
		this.tableId = tableId;
	}

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					V_tables frame = new V_tables();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public V_tables() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(V_tables.class.getResource("/images/bookTypeManager.png")));
		setTitle("餐桌列表");
		setBackground(new Color(240, 240, 240));
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 900, 500);
		contentPane = new JPanel();
		contentPane.setToolTipText("");
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		List<Table> tableList=new ArrayList<>();
		tabbedPane.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				if(tabbedPane.getSelectedIndex()==1) {
					List<Table> tableList=new ArrayList<>();
					tableList=ts.findBysattus(0);
					showTable(tableList,panel_2);//空闲桌子展示
				}else {
					List<Table> tableList=new ArrayList<>();//全部桌子展示
					tableList=ts.findAll();
					showTable(tableList,panel);
				}
			}
		});
		tabbedPane.setBounds(10, 10, 860, 440);
		contentPane.add(tabbedPane);
		
		 panel = new JPanel();
		tabbedPane.addTab("全部餐桌", null, panel, null);
		
		panel_2 = new JPanel();
		tabbedPane.addTab("空闲餐桌", null, panel_2, null);
		
		
		
	}
	
	
	public void showTable(List<Table> list,JPanel panel) {
		System.out.println("-------------------------");
		//循环=================开始
		JPopupMenu popupMenu = new JPopupMenu();
		addPopup(panel, popupMenu);
		
		JMenuItem menuItem = new JMenuItem("选择");
		menuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//如果被选中得到的是桌子表的id
				tableId=CastUtil.castInt(this_panle.getToolTipText());
				Table selectTable=ts.findById(tableId);
				selectTable.setStatus(1);
				if(ts.update(selectTable)) {
					JOptionPane.showMessageDialog(V_tables.this, selectTable.getNode()+"已被选择");
				}else {
					JOptionPane.showMessageDialog(V_tables.this, "选择桌子失败");
				}
				this_panle.setVisible(false);
				dispose();
			}
		});
		popupMenu.add(menuItem);
		
		JMenuItem menuItem_2 = new JMenuItem("更换餐桌");
		menuItem_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int id=CastUtil.castInt(this_panle.getToolTipText());
				Table selectTable=ts.findById(id);
				System.out.println(selectTable.getStatus());
				if(selectTable.getStatus()==1) {
					selectTable.setStatus(0);
					if(ts.update(selectTable)) {
						int msg = JOptionPane.showConfirmDialog(V_tables.this, selectTable.getNode()+"已解除占用","更换桌子",JOptionPane.YES_OPTION);
						System.out.println(msg+"msg");
						//监听弹框关闭，初始化table页面
//						if(msg==0) {
							List<Table> tableList=new ArrayList<>();//全部桌子展示
							tableList=ts.findAll();
							showTable(tableList,panel);
//						}
					}else {
						JOptionPane.showMessageDialog(V_tables.this, "更换失败");
					}
				}else {
					JOptionPane.showMessageDialog(V_tables.this, "桌子未占用");
				}
				
			}
		});
		popupMenu.add(menuItem_2);
		
		JMenuItem menuItem_1 = new JMenuItem("删除");
		menuItem_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int id=CastUtil.castInt(this_panle.getToolTipText());
				ts.delete(id);
				this_panle.setVisible(false);	
				
				JOptionPane.showMessageDialog(V_tables.this, "删除成功");
			}
		});
		popupMenu.add(menuItem_1);
		panel.removeAll();
		panel.setLayout(null);
		
				int y = 10;
				int x = 10;
				for (int i = 1; i <= list.size(); i++) {
					JPanel panel_1 = new JPanel();
					panel_1.setToolTipText(list.get(i-1).getId()+"");
					panel_1.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
					panel_1.setBounds(x, y, 160, 130);
					panel.add(panel_1);
					panel_1.setLayout(null);
					//设置桌子编号
					int num=list.get(i-1).getStatus();
					JLabel label = new JLabel();
					label.setForeground(Color.RED);
					if(num==0) {
						label.setText("空闲");
					}else {
						label.setText("使用中");
					}
					label.setBounds(10, 10, 54, 15);
					panel_1.add(label);
					//桌子图片
					 labelIcon=new JLabel();
					 
					
					 
					ImageIcon icon=new ImageIcon(list.get(i-1).getIcon());
					icon.setImage(icon.getImage().getScaledInstance(130, 100, 1));
					labelIcon.setIcon(icon);
					labelIcon.setBounds(10,10,160,80);
					panel_1.add(labelIcon);
					//设置桌子名称
					 label_1 = new JLabel(list.get(i-1).getNode());
					label_1.setBounds(50, 100, 54, 15);
					
					panel_1.add(label_1);
					if(i % 5 == 0) {
						x = 10;
						y+=130;
					}else {
						x += 170;
					}
					
					addPopup(panel_1, popupMenu);
				}
				//循环----------------结束
				update(getGraphics());//窗体组件重绘
				
	}
	
	
	private static void addPopup(Component component, final JPopupMenu popup) {
		
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			private void showMenu(MouseEvent e) {
				 this_panle = (JPanel) e.getSource();
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}
}
